import {
  list,
  findByName
} from '../../api/wxPage'
import {
  list as designerList
} from '../../api/designer'
import {
  lockSysUser,
  scaner
} from '../../api/member'
import {
  popScreen,
  clickAndGet
} from '../../api/promotion'
import {
  LBSLocation
} from '../../api/amap'
//获取应用实例
const app = getApp()
const util = require('../../utils/util');
Page({
  data: {
    title: '吃莆田',
    imageIndex:0,
    isChoose: 0,
    pageNum: 0,
    pageSize: 10,
    transparent: 1,
    indexType: false,
    current: 0,
    headerHeight: 0,
    backgroundImage: ['','',''],
    opacityIs: false,
    contentHeight: 0,
    contentHeaderHeight: 0,
    pageHeight: 0,
    scrollHeight: 0,
    contentHeaderType: false,
    windowHeight: 0,
    totalHeight: 300,
    logoHg: 0,
    hiddenHeader: 0,
    hiddenHeaderType: false,
    bgSrc: "http://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/meishi.png",
    centerHg: 0,
    heightHidden: 0,
    totalHeightTwo: 0,
    totalHeightThree: 0,
    componentsList: [
      // {name:'dinnerSwiper'},
      // {name:'fourAd'},
      // {name:'foodAd'},
      // {name:'foodList'}
    ],
    componentsListTwo: [{
        name: 'header'
      },
      {
        name: 'travelList'
      }
    ],
    componentsListThree: [{
        name: 'hotpost'
      },
      {
        name: 'watchList'
      }
    ],
    indexAddress: '',
    promotionForm: {},
    promotionPrice: 0
  },
  onReady: function () {

    var _this = this

  },
  onLoad: function () {
    this.data.windowHeight = wx.getSystemInfoSync().windowHeight
    this.setNavSize()
    var _this = this
    _this.addressClick()
    _this.restaurantFind()
    this.travelFind()
    this.hotspotFind()
    var query = wx.createSelectorQuery();
    //选择id
    // 屏幕高度


    this.setData({
      windowHeight: this.data.windowHeight
    })
    // query.select('.headerBox').boundingClientRect(function (rect) {
    //   // 头部高度
    //   _this.data.headerHeight = rect.height
    //   _this.setData({
    //     headerHeight: _this.data.headerHeight
    //   })
    // }).exec();
    // query.select('.contentHeader').boundingClientRect(function (rect) {
    //   // 去除头部、内容头部剩余高度
    //   _this.data.contentHeaderHeight = wx.getSystemInfoSync().windowHeight - rect.height - _this.data.headerHeight
    //   _this.data.contentHeight = wx.getSystemInfoSync().windowHeight - rect.height
    //   _this.data.pageHeight = wx.getSystemInfoSync().windowHeight + rect.height
    //   _this.data.centerHg = rect.height
    //   _this.setData({
    //     contentHeaderHeight: _this.data.contentHeaderHeight,
    //     contentHeight: _this.data.contentHeight,
    //     pageHeight: _this.data.pageHeight,
    //     centerHg: _this.data.centerHg
    //   })
    // }).exec();
    // 内容高度

    // 头像高度
    // query.select('.logoHeight').boundingClientRect(function (rect) {
    //   _this.data.logoHg = rect.height
    // }).exec();
    // 头部隐藏条高度
    // query.select('.HeightHidden').boundingClientRect(function (rect) {
    //   _this.data.heightHidden = rect.height
    //   _this.setData({
    //     heightHidden: _this.data.heightHidden
    //   })
    // }).exec();

    app.userInfoReadyCallback = res => {

      _this.popScreenClick()

    }
  },
  onShow() {
    var _this = this
    // _this.popScreenClick()
    // this.getHotHeight()
    // this.getLvyouHeight()
    // this.getCanHeight()
    _this.setData({
      indexAddress: app.globalData.addresstotalHeightTwo
    })
  },
  // tab切换
  selectKind(data) {
    this.data.imageIndex=data.currentTarget.dataset.index
    this.data.isChoose = data.currentTarget.dataset.index
    this.setData({
      imageIndex:this.data.imageIndex,
      isChoose: this.data.isChoose
    })
  },

  // 点击tab切换
  bindChange(e) {
    
   this.data.isChoose= e.detail.current 
   this.setData({
    isChoose:this.data.isChoose
   })
  },
  // 搜索
  searchClick() {
    wx.navigateTo({
      url: '/pages/goods/search/index'
    })
  },
  // 状态栏高度
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    console.log(sysinfo)
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  //跳活动页
  toActivity() {
    wx.navigateTo({
      url: '/pages/coupon/coupon'
    })
  },
  // 优惠券
  couponClick() {
    this.selectComponent("#coupon").couponClick()
  },
  // 手指放开
  touchEnd: function (e) {
    // this.pageScrollClick()
  },
  // 滚动监听
  onPageScroll: function (e) {
    var _this = this
    _this.data.scrollHeight = e.scrollTop


    // if (e.scrollTop >= _this.data.contentHeaderHeight - 100 && _this.data.contentHeaderType == false) {
    //   _this.data.contentHeaderType = true
    //   _this.setData({
    //     contentHeaderType: _this.data.contentHeaderType
    //   })
    // } else if (e.scrollTop < _this.data.contentHeaderHeight - 100 && _this.data.contentHeaderType == true) {
    //   _this.data.contentHeaderType = false
    //   _this.setData({
    //     contentHeaderType: _this.data.contentHeaderType
    //   })
    // }
    // if(e.scrollTop>(_this.data.contentHeaderHeight -60)&&_this.data.opacityIs==false){
    //   _this.data.opacityIs=true
    //   _this.setData({
    //     opacityIs:_this.data.opacityIs
    //   })
    //   console.log(_this.data.opacityIs)
    // } 
    // if(e.scrollTop<(_this.data.contentHeaderHeight -60)&&_this.data.opacityIs==true){
    //   _this.data.opacityIs=false
    //   _this.setData({
    //     opacityIs:_this.data.opacityIs
    //   })
    //   console.log(_this.data.opacityIs)
    // }
    if (e.scrollTop > 130 && _this.data.hiddenHeaderType == false) {
      _this.data.hiddenHeaderType = true
      _this.setData({
        hiddenHeaderType: _this.data.hiddenHeaderType
      })
    } else if (e.scrollTop < 130 && _this.data.hiddenHeaderType == true) {
      _this.data.hiddenHeaderType = false
      _this.setData({
        hiddenHeaderType: _this.data.hiddenHeaderType
      })
    }
  },
  // 吸顶事件
  pageScrollClick(data) {
    var _this = this
    if (_this.data.scrollHeight < _this.data.contentHeaderHeight) {
      console.log(_this.data.scrollHeight)
      if (_this.data.indexType == false && _this.data.scrollHeight > 50) {
        console.log('进来了')
        wx.pageScrollTo({
          scrollTop: _this.data.contentHeaderHeight,
          duration: 200
        })
        _this.data.indexType = true
      } else if (_this.data.indexType == true && _this.data.contentHeaderHeight - 100 > _this.data.scrollHeight) {
        _this.data.indexType = false
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 200
        })
      } else if (_this.data.indexType == false && _this.data.scrollHeight < 100) {
        // _this.data.indexType = false
        // wx.pageScrollTo({
        //   scrollTop: 0,
        //   duration: 200
        // })
      } else if (_this.data.indexType == true && _this.data.contentHeaderHeight - 100 < _this.data.scrollHeight) {
        // wx.pageScrollTo({
        //   scrollTop: _this.data.contentHeaderHeight,
        //   duration: 200
        // })
        // _this.data.indexType = true
      }
    }
  },
  
  // 点击上划
  contentHeaderClick() {
    var _this = this
    wx.pageScrollTo({
      scrollTop: _this.data.contentHeaderHeight + 200,
      duration: 500
    })
    _this.data.indexType = true
  },
  // 餐饮设计器
  restaurantFind() {
    var _this = this
    findByName({
      name: '餐饮'
    }).then(
      res => {
        if (!util.isNull(res.data.thumbnial)) {
          res.data.thumbnial = util.thumbnail(res.data.thumbnial, 750, 560)
          this.data.backgroundImage[0]=res.data.thumbnial
        }
        this.setData({
          backgroundImage: this.data.backgroundImage
        })
        _this.restaurantDesignerList(res.data.id)
      })
  },
  restaurantDesignerList(id) {
    designerList({
      pageId: id
    }).then(res => {
      this.data.componentsList = res.data
      this.setData({
        componentsList: this.data.componentsList
      })
    })
  },
  // 旅游
  travelFind() {
    var _this = this
    findByName({
      name: '旅游'
    }).then(
      res => {
        if (!util.isNull(res.data.thumbnial)) {
          res.data.thumbnial = util.thumbnail(res.data.thumbnial, 750, 560)
          this.data.backgroundImage[1]=res.data.thumbnial 
        }
        this.setData({
          backgroundImage: this.data.backgroundImage
        })
        _this.travelDesignerList(res.data.id)
      })
  },
  travelDesignerList(id) {
    designerList({
      pageId: id
    }).then(res => {
      this.data.componentsListTwo = res.data
      this.setData({

        componentsListTwo: this.data.componentsListTwo
      })
      // setTimeout(() => {
      //   this.getLvyouHeight()
      // }, 300);

    })
  },
  // 热点
  hotspotFind() {
    var _this = this
    findByName({
      name: "热点"
    }).then(
      res => {
        if (!util.isNull(res.data.thumbnial)) {
          res.data.thumbnial = util.thumbnail(res.data.thumbnial, 750, 560)
          this.data.backgroundImage[2]=res.data.thumbnial 
        }
        this.setData({
          backgroundImage: this.data.backgroundImage
        })
        _this.hotspotList(res.data.id)
      })
  },
  hotspotList(id) {
    designerList({
      pageId: id
    }).then(res => {
      this.data.componentsListThree = res.data
      this.setData({
        componentsListThree: this.data.componentsListThree
      })
      // setTimeout(() => {
      //   this.getHotHeight()
      // }, 300);

    })
  },

  //获取弹屏活动
  popScreenClick() {
    var _this = this
    _this.data.promotionPrice = 0
    popScreen({
      mchId: app.globalData.mchId
    }).then(res => {
      console.log(res)
      if (res.content != '无活动') {
        if (res.data.itemList.length > 0) {
          res.data.itemList.forEach(function (item) {
            _this.data.promotionPrice += item.amount
          })
          _this.data.promotionForm = res.data
          _this.couponClick()
          _this.setData({
            promotionForm: _this.data.promotionForm,
            promotionPrice: _this.data.promotionPrice
          })
        } else {
          // _this.data.newPromotion = false
          // _this.setData({
          //   newPromotion: _this.data.newPromotion
          // })
        }
      } else {
        // _this.data.newPromotion = false
        // _this.setData({
        //   newPromotion: _this.data.newPromotion
        // })
      }
    })
  },
  clickAndGetClick() {
    var _this = this
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
      return;
    }
    // 弹屏领取
    clickAndGet({
      promotionId: _this.data.promotionForm.promotion.id
    }).then(res => {
      console.log(res)
      _this.data.newPromotion = false
      _this.setData({
        newPromotion: _this.data.newPromotion
      })
      wx.navigateTo({
        url: '/pages/member/coupon/coupon',
        success: function (res) {},
        fail: function (res) {},
        complete: function (res) {},
      })
    })
  },
  changeAddress() {
    wx.navigateTo({
      url: '/pages/member/address/address?type=change',
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {


      },
    })
  },
  addressClick() {

    var _this = this
    wx.getLocation({
      type: 'gcj02 ', //返回可以用于wx.openLocation的经纬度
      isHighAccuracy:true,
      success(res) {
        console.log("小程序获取的经纬度",res)
        const latitude = res.latitude
        const longitude = res.longitude
        LBSLocation({
          lat: res.latitude,
          lng: res.longitude
        }).then(ress => {
          console.log("后台获取的数据",ress)
          _this.data.indexAddress = ress.data.address
          wx.setStorageSync('currentAreaId', ress.data.areaId)
          _this.setData({
            indexAddress: _this.data.indexAddress
          })
        })
      }
    })
  },
  //下来刷新
  onPullDownRefresh() {
    var _this = this
    wx.showNavigationBarLoading()
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      if (_this.data.isChoose == 0) {
        _this.selectComponent('#foodList').downRefresh();
        setTimeout(() => {
          wx.stopPullDownRefresh()
        }, 300)
      } else if (_this.data.isChoose == 1) {
        _this.selectComponent('#travelList').downRefresh();
        setTimeout(() => {
          wx.stopPullDownRefresh()
        }, 300)
      } else if (_this.data.isChoose == 2) {
        _this.selectComponent('#watchList').downRefresh();
        setTimeout(() => {
          wx.stopPullDownRefresh()
        }, 300)
      }
    }, 500)
  },
  //上拉加载
  onReachBottom() {
    if (this.data.isChoose == 0) {
      if (this.data.componentsList[this.data.componentsList.length - 1].name == 'foodList') {
        this.selectComponent('#foodList').onReachBottom();
      }
    } else if (this.data.isChoose == 1) {
      if (this.data.componentsListTwo[this.data.componentsListTwo.length - 1].name == 'travelList') {
        this.selectComponent('#travelList').onReachBottom();
      }
    } else if (this.data.isChoose == 2) {
      if (this.data.componentsListThree[this.data.componentsListThree.length - 1].name == 'watchList') {
        this.selectComponent('#watchList').onReachBottom();
      }
    }
  },
  onShareAppMessage() {
    return {
      imageUrl: util.thumbnail("https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/fenxiangtu.jpg", 500, 400)
    }
  },
  onShareTimeline() {

  }
})